package cn.buk.tms.eotms.controller;

import cn.buk.common.dto.CommonDto;
import cn.buk.common.sc.CommonSearchCriteria;
import cn.buk.tms.eotms.entity.Pnr;
import cn.buk.tms.eotms.service.PnrService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 *
 * @author yfdai
 * @date 2017/1/10
 */
@RestController
@RequestMapping("/pnr")
public class PnrController extends BaseController {

    private static final Logger logger = Logger.getLogger(PnrController.class);

    @Autowired
    private PnrService pnrService;


    @Override
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));//true:允许输入空值，false:不能为空值
    }

    @Override
    @InitBinder("sc")
    public void initBinder1(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("sc.");
    }


    /**
     * 查找PNR
     * @param request
     * @param sc
     * @return
     */
    @RequestMapping("/list")
    public CommonDto<Pnr> searchPnr(HttpServletRequest request,
                                    @ModelAttribute("sc") CommonSearchCriteria sc) {

        List<Pnr> pnrs = pnrService.searchPnr(getUserId(request), sc);

        CommonDto<Pnr> pnrCommonDto = new CommonDto<>();
        pnrCommonDto.setDataList(pnrs);
        pnrCommonDto.setPage(sc.getPage());

        return pnrCommonDto;
    }

    @RequestMapping("/{id}")
    public Pnr getPnrDetail(HttpServletRequest request, @PathVariable("id") final int id) {
        final int enterpriseId = getEnterpriseId(request);

        return pnrService.getPnrById(id, enterpriseId);
    }

}
